package com.wing.productionPlan.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.model.entity.DayTakeGoodsIterm;
import com.wing.productionPlan.model.entity.DayTakeGoodsProduct;
import com.wing.productionPlan.model.response.DayTakeGoodsItermVO;
import com.wing.productionPlan.model.response.DayTakeGoodsProductVO;
import com.wing.productionPlan.model.response.DayTakeGoodsVO;
import com.wing.productionPlan.service.DayTakeGoodsItermService;
import com.wing.productionPlan.service.DayTakeGoodsProductService;
import com.wing.productionPlan.service.DayTakeGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Validated
@Api(value = "Admin-生产管理-日提货订单 产品详情", tags = "#/product/requirePlan/dayTakeGoodsProductList")
@RestController("DayTakeGoodsProductController")
@RequestMapping("productionPlan/admin/dayTakeGoodsProduct")
@RequiredArgsConstructor
public class DayTakeGoodsProductController {


    private final DayTakeGoodsProductService dayTakeGoodsProductService;

    private final DayTakeGoodsItermService dayTakeGoodsItermService;

    @ApiOperation(value = "查询日提货明细 产品列表", notes = "搜索列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<DayTakeGoodsProductVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String takeOrderNumber
            ,String factoryName
            ,String startCreateTime
            ,String endCreateTime
    ) {
        Page<DayTakeGoodsProductVO> pg = new Page<>(page, limit);
        HashMap map = new HashMap();
        map.put("takeOrderNumber", takeOrderNumber);
        map.put("factoryName", factoryName);
        if (StringUtils.isNotBlank(startCreateTime)) {
            map.put("startCreateTime", startCreateTime);
        }
        if (StringUtils.isNotBlank(endCreateTime)) {
            map.put("endCreateTime", endCreateTime);
        }
        Page<DayTakeGoodsProductVO> list = dayTakeGoodsProductService.pageList4app(pg, map);
        List<DayTakeGoodsProductVO> records = list.getRecords();

        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "查询日提货明细 产品列表详情", notes = "搜索")
    @GetMapping("/getTakeGoodsProductDetail")
    public JsonResult getTakeGoodsProductDetail(@RequestParam("id") Long id) {
        DayTakeGoodsProductVO planItermById = dayTakeGoodsProductService.getTakeGoodsProductById(id);
        //DayTakeGoodsProductVO appById = dayTakeGoodsProductService.getTakeGoodsProductById(id);
        if(planItermById!=null){
            /* appById.setAuditStatusText(AuditStatusEnum.getDescriptionByCode(appById.getAuditStatus()));*/
            LambdaQueryWrapper<DayTakeGoodsIterm> wrapper1 = Wrappers.<DayTakeGoodsIterm>lambdaQuery()
                    .eq(DayTakeGoodsIterm::getPlantCode, planItermById.getPlantCode())
                    .eq(DayTakeGoodsIterm::getPurchaseTissue, planItermById.getPurchaseTissue())
                    .eq(DayTakeGoodsIterm::getReceiveMouthDate, planItermById.getReceiveMouthDate())
                    .eq(DayTakeGoodsIterm::getReceiveMouthDay, planItermById.getReceiveMouthDay())
                    .eq(DayTakeGoodsIterm::getSkuCode, planItermById.getSkuCode())
                    .eq(DayTakeGoodsIterm::getProductLine, planItermById.getProductLine());

            List<DayTakeGoodsIterm> dayTakeGoodsProductsList = dayTakeGoodsItermService.list(wrapper1);
            planItermById.setDayTakeGoodsItermList(dayTakeGoodsProductsList);
            return JsonResult.success(planItermById);
        }else{
            return JsonResult.fail("未发现详情数据！");
        }
     }


}
